import request from '@/utils/request'

// 获取模板列表
export function listTemplates() {
  return request({
    url: '/system/template/list',
    method: 'get'
  })
}

// 获取报告统计信息
export function getReportStatistics() {
  return request({
    url: '/system/report/v1/statistics',
    method: 'get'
  })
}

// 获取业务指标详细统计
export function getBusinessMetrics() {
  return request({
    url: '/system/report/v1/business-metrics',
    method: 'get'
  })
}

// 获取报告历史列表
export function getReportHistory(params) {
  return request({
    url: '/system/report/v1/history',
    method: 'get',
    params: params
  })
}

// 智能收集素材
export function intelligentCollect(data) {
  return request({
    url: '/system/material/listByDateRange',
    method: 'get',
    params: {
      startDate: data.dateRange[0],
      endDate: data.dateRange[1],
      keyword: data.keyword || ''
    }
  })
}

// 生成初稿
export function generateDraft(data) {
  return request({
    url: '/system/report/v1/generate',
    method: 'post',
    data: {
      templateId: data.template,
      type: data.reportType,
      startDate: data.dateRange[0],
      endDate: data.dateRange[1],
      materials: data.materials,
      advanced: data.advanced || {}
    }
  })
}

// 保存报告
export function saveReport(data) {
  return request({
    url: '/system/report/v1/api/add',
    method: 'post',
    data: data
  })
}

// 更新报告
export function updateReport(data) {
  return request({
    url: '/system/report/v1/api/update',
    method: 'put',
    data: data
  })
}

// 删除报告
export function deleteReport(reportId) {
  return request({
    url: '/system/report/v1/api/delete/' + reportId,
    method: 'delete'
  })
}

// 批量删除报告
export function batchDeleteReport(reportIds) {
  return request({
    url: '/system/report/v1/api/batch/' + reportIds.join(','),
    method: 'delete'
  })
}

// 获取报告详情
export function getReport(reportId) {
  return request({
    url: '/system/report/v1/api/info/' + reportId,
    method: 'get'
  })
}

// 导出报告
export function exportReport(reportId, format = 'markdown') {
  return request({
    url: '/system/report/v1/export/' + reportId,
    method: 'get',
    params: { format },
    responseType: 'blob'
  })
}

// 导入报告模板
export function importTemplate(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/system/template/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 导出报告模板
export function exportTemplate(templateId) {
  return request({
    url: '/system/template/export/' + templateId,
    method: 'get',
    responseType: 'blob'
  })
}

// 获取素材分类统计
export function getMaterialStatistics(dateRange) {
  return request({
    url: '/system/material/statistics',
    method: 'get',
    params: {
      startDate: dateRange[0],
      endDate: dateRange[1]
    }
  })
}

// 批量操作素材
export function batchOperateMaterials(operation, materialIds) {
  return request({
    url: '/system/material/batch',
    method: 'post',
    data: {
      operation: operation,
      materialIds: materialIds
    }
  })
}

// 获取AI生成建议
export function getAISuggestions(materials, reportType) {
  return request({
    url: '/system/report/v1/suggestions',
    method: 'post',
    data: {
      materials: materials,
      reportType: reportType
    }
  })
}

// 验证报告内容
export function validateReport(content) {
  return request({
    url: '/system/report/v1/validate',
    method: 'post',
    data: {
      content: content
    }
  })
}

// 获取报告模板预览
export function getTemplatePreview(templateId) {
  return request({
    url: '/system/template/preview/' + templateId,
    method: 'get'
  })
}

// 设置报告提醒
export function setReportReminder(data) {
  return request({
    url: '/system/report/v1/reminder',
    method: 'post',
    data: data
  })
}

// 获取报告提醒列表
export function getReportReminders() {
  return request({
    url: '/system/report/v1/reminders',
    method: 'get'
  })
}

// 删除报告提醒
export function deleteReportReminder(reminderId) {
  return request({
    url: '/system/report/v1/reminder/' + reminderId,
    method: 'delete'
  })
}

// 获取知识库搜索结果
export function searchKnowledgeBase(query, filters = {}) {
  return request({
    url: '/system/report/v1/knowledge/search',
    method: 'get',
    params: {
      query: query,
      ...filters
    }
  })
}

// 获取知识库标签列表
export function getKnowledgeBaseTags() {
  return request({
    url: '/api/v1/rag/query_rag_tag_list',
    method: 'get'
  })
}

// 查询指定知识库内容
export function queryKnowledgeBaseContent(ragTag) {
  return request({
    url: '/api/v1/rag/query_rag_content',
    method: 'get',
    params: { ragTag }
  })
}

// 智能搜索知识库（用于报告生成）
export function intelligentKnowledgeSearch(query, options = {}) {
  return request({
    url: '/api/v1/rag/search',
    method: 'post',
    data: {
      query: query,
      maxResults: options.maxResults || 10,
      similarityThreshold: options.similarityThreshold || 0.7,
      knowledgeTags: options.knowledgeTags || [],
      ...options
    }
  })
}

// 获取知识库统计信息
export function getKnowledgeBaseStatistics() {
  return request({
    url: '/api/v1/rag/stats',
    method: 'get'
  })
}

// 批量查询多个知识库
export function batchQueryKnowledgeBases(ragTags) {
  return request({
    url: '/api/v1/rag/batch_query',
    method: 'post',
    data: { ragTags }
  })
}

// 获取报告生成进度
export function getReportProgress(taskId) {
  return request({
    url: '/system/report/v1/progress/' + taskId,
    method: 'get'
  })
}

// 取消报告生成
export function cancelReportGeneration(taskId) {
  return request({
    url: '/system/report/v1/cancel/' + taskId,
    method: 'post'
  })
}

// 获取报告质量评分
export function getReportQualityScore(reportId) {
  return request({
    url: '/system/report/v1/quality/' + reportId,
    method: 'get'
  })
}

// 提交报告反馈
export function submitReportFeedback(reportId, feedback) {
  return request({
    url: '/system/report/v1/feedback/' + reportId,
    method: 'post',
    data: feedback
  })
}

// 获取报告协作信息
export function getReportCollaboration(reportId) {
  return request({
    url: '/system/report/v1/collaboration/' + reportId,
    method: 'get'
  })
}

// 添加报告协作者
export function addReportCollaborator(reportId, userId) {
  return request({
    url: '/system/report/v1/collaboration/' + reportId,
    method: 'post',
    data: {
      userId: userId
    }
  })
}

// 移除报告协作者
export function removeReportCollaborator(reportId, userId) {
  return request({
    url: '/system/report/v1/collaboration/' + reportId + '/' + userId,
    method: 'delete'
  })
}

// 获取报告版本历史
export function getReportVersions(reportId) {
  return request({
    url: '/system/report/v1/versions/' + reportId,
    method: 'get'
  })
}

// 恢复报告版本
export function restoreReportVersion(reportId, versionId) {
  return request({
    url: '/system/report/v1/restore/' + reportId + '/' + versionId,
    method: 'post'
  })
}

// 比较报告版本
export function compareReportVersions(reportId, version1, version2) {
  return request({
    url: '/system/report/v1/compare/' + reportId,
    method: 'get',
    params: {
      version1: version1,
      version2: version2
    }
  })
} 